package com.gxa.company.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class MyException {

    /**
     * 捕获所有异常
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    Object exception(Exception e , HttpServletRequest request){
        Map<String,Object> map = new HashMap<>(16);
        map.put("code",500);
        map.put("msg",e.getMessage());
        map.put("data",request.getRequestURI());
        return map;
    }

    /**
     * 捕获自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = SystemException.class)
    Object MyException(SystemException e){
        Map<String,Object> map = new HashMap<>(16);
        map.put("code",e.getCode());
        map.put("msg",e.getMsg());
        map.put("data",null);
        return map;
    }
}
